﻿package mortal.game.view.pack
{
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.debug.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import com.mui.manager.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.cd.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.common.menu.*;
    import mortal.game.view.pack.data.*;
    import mortal.game.view.pack.social.*;
    import mortal.mvc.core.*;

    public class PackItem extends BaseItem
    {
        private var _notMeetCareerEffect:ScaleBitmap;
        private var _isNotMeet:Boolean;
        private var _tradingIcon:ScaleBitmap;
        private var _isTrading:Boolean;
        private var _data:PackObject;

        public function PackItem()
        {
            return;
        }// end function

        override protected function updateView() : void
        {
            super.updateView();
            if (this._notMeetCareerEffect)
            {
                this._notMeetCareerEffect.width = _width - _paddingLeft * 2;
                this._notMeetCareerEffect.height = _height - _paddingTop * 2;
                this._notMeetCareerEffect.x = _paddingLeft;
                this._notMeetCareerEffect.y = _paddingTop;
            }
            if (this._tradingIcon)
            {
                this._tradingIcon.width = _width - _paddingLeft * 2;
                this._tradingIcon.height = _height - _paddingTop * 2;
                this._tradingIcon.x = _paddingLeft;
                this._tradingIcon.y = _paddingTop;
            }
            if (_txtStrenLv)
            {
                _txtStrenLv.x = this.width - _txtStrenLv.width - _paddingLeft;
                _txtStrenLv.y = this.height - _txtStrenLv.height - 2 - _paddingTop;
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            _isShowFreezingEffect = true;
            this.doubleClickEnabled = true;
            this.isShowLock = true;
            this._isCheckOtherSkillBookState = true;
            _isShowLeftTimeEffect = false;
            this.canNotUse = false;
            this.isNotMeetCarrer = false;
            this.isTrading = false;
            this.buttonMode = true;
            this.setListener();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            if (this._notMeetCareerEffect)
            {
                this._notMeetCareerEffect.dispose(param1);
                this._notMeetCareerEffect = null;
            }
            if (this._tradingIcon)
            {
                this._tradingIcon.dispose(param1);
                this._tradingIcon = null;
            }
            if (_freezingEffect)
            {
                _freezingEffect.alpha = 1;
            }
            this._data = null;
            ItemMenuRegister.unRegister(this);
            super.disposeImpl(param1);
            return;
        }// end function

        override protected function resetEffectPlace() : void
        {
            if (_freezingEffect)
            {
                _freezingEffect.alpha = 0.6;
                _freezingEffect.setMaskSize(_width - _paddingLeft * 2, _height - _paddingTop * 2);
                _freezingEffect.x = _width / 2;
                _freezingEffect.y = _height / 2;
            }
            if (_leftTimeEffect)
            {
                _leftTimeEffect.width = _width;
                _leftTimeEffect.height = 20;
                _leftTimeEffect.x = 0;
                _leftTimeEffect.y = (_height - _leftTimeEffect.height) / 2;
            }
            return;
        }// end function

        override public function set pos(param1:int) : void
        {
            _pos = param1;
            if (this._data)
            {
                this._data.pos = _pos;
            }
            return;
        }// end function

        public function set isNotMeetCarrer(param1:Boolean) : void
        {
            this._isNotMeet = param1;
            if (this._isNotMeet)
            {
                if (!this._notMeetCareerEffect)
                {
                    this._notMeetCareerEffect = ResourceConst.getScaleBitmap(ImagesConst.PackDisable);
                    _middleLayer.addChild(this._notMeetCareerEffect);
                    this._notMeetCareerEffect.width = _width - _paddingLeft * 2;
                    this._notMeetCareerEffect.height = _height - _paddingTop * 2;
                    this._notMeetCareerEffect.x = _paddingLeft;
                    this._notMeetCareerEffect.y = _paddingTop;
                }
            }
            if (this._notMeetCareerEffect)
            {
                this._notMeetCareerEffect.visible = this._isNotMeet;
                if (GameController.trade.isViewShow)
                {
                    this._notMeetCareerEffect.visible = false;
                }
            }
            return;
        }// end function

        public function set isTrading(param1:Boolean) : void
        {
            this._isTrading = param1;
            if (this._isTrading)
            {
                if (!this._tradingIcon)
                {
                    this._tradingIcon = ResourceConst.getScaleBitmap(ImagesConst.PackTrade);
                    _middleLayer.addChild(this._tradingIcon);
                    this._tradingIcon.width = _width - _paddingLeft * 2;
                    this._tradingIcon.height = _height - _paddingTop * 2;
                    this._tradingIcon.x = _paddingLeft;
                    this._tradingIcon.y = _paddingTop;
                }
            }
            if (this._tradingIcon)
            {
                this._tradingIcon.visible = GameController.trade.isViewShow;
                this._tradingIcon.visible = this._isTrading;
            }
            return;
        }// end function

        private function setListener() : void
        {
            this.configEventListener(MouseEvent.CLICK, this.clickHandler);
            this.configEventListener(MouseEvent.DOUBLE_CLICK, this.fastOperate);
            return;
        }// end function

        override public function set itemData(param1:ItemData) : void
        {
            var arr:Array;
            var item:ItemData;
            var i:int;
            var value:* = param1;
            ItemMenuRegister.unRegister(this);
            this.isDragAble = false;
            this.isDropAble = false;
            this.isNotMeetCarrer = false;
            this.isBind = false;
            this.isPrescritime = false;
            this.amount = 0;
            EffectManager.glowFilterUnReg(this);
            this.removeEventListener(MouseEvent.ROLL_OVER, this.showUnLockIcon);
            this.removeEventListener(MouseEvent.ROLL_OUT, this.hideUnLockIcon);
            updateCDEffect();
            if (value == PackItemData.lockItemData)
            {
                this.source = GlobalClass.getBitmap(ImagesConst.Locked2);
                _itemData = value;
                this.configEventListener(MouseEvent.ROLL_OVER, this.showUnLockIcon);
                this.configEventListener(MouseEvent.ROLL_OUT, this.hideUnLockIcon);
            }
            else if (value == PackItemData.unLockItemData)
            {
                this.source = GlobalClass.getBitmap(ImagesConst.UnLock2);
                _itemData = value;
                EffectManager.glowFilterReg(this);
            }
            else if (value == PackItemData.unLockingItemData)
            {
                this.source = GlobalClass.getBitmap(ImagesConst.UnLocking2);
                _itemData = value;
                _cdData = Cache.instance.cd.getCDData("lockCD", CDDataType.backPackLock);
                updateCDEffect("lockCD", CDDataType.backPackLock);
                this.configEventListener(MouseEvent.ROLL_OVER, this.showUnLockIcon);
                this.configEventListener(MouseEvent.ROLL_OUT, this.hideUnLockIcon);
            }
            else if (value == PackItemData.needMoneyItemData)
            {
                this.source = GlobalClass.getBitmap(ImagesConst.WarehouseLock);
                _itemData = value;
                this.configEventListener(MouseEvent.ROLL_OVER, this.showUnLockIcon);
                this.configEventListener(MouseEvent.ROLL_OUT, this.hideUnLockIcon);
            }
            else if (value)
            {
                _itemData = value;
                ItemMenuRegister.register(this, this.itemData);
                this.source = value.itemInfo.url;
                this.amount = value.serverData.itemAmount;
                this.isShowToolTip = true;
                this.isDragAble = true;
                this.isDropAble = true;
                this.isNotMeetCarrer = !ItemsUtil.isFitCarrer(itemData);
                this.isBind = ItemsUtil.isBind(this.itemData);
                this.isPrescritime = ItemsUtil.isPrescriptionItem(value);
                updateCDEffect(this.itemData, CDDataType.itemData);
                if (ItemsUtil.isGiftBag(_itemData))
                {
                    var deleteItem:* = function () : void
            {
                arr.splice(i, 1);
                return;
            }// end function
            ;
                    arr = GameController.pack.giftItemArr;
                    while (i < arr.length)
                    {
                        
                        item = arr[i];
                        if (ItemsUtil.isMoneyCard(item))
                        {
                        }
                        else if (item.serverData.uid == _itemData.serverData.uid)
                        {
                            EffectManager.glowFilterReg(this, null, 1, 10, 0, 5, deleteItem);
                            break;
                        }
                        i = (i + 1);
                    }
                    if (ItemsUtil.isCanUseMoneyCard(_itemData))
                    {
                        EffectManager.glowFilterReg(this);
                    }
                }
            }
            else
            {
                this.source = null;
                _itemData = null;
                this.isDropAble = true;
                this.buttonMode = false;
            }
            updateColorShow();
            updateStrengLevel();
            updateAroundEffect();
            checkMountSkillBookState();
            return;
        }// end function

        override protected function cdFinishedHandler() : void
        {
            unRegisterEffects();
            this.getTime();
            return;
        }// end function

        private function showUnLockIcon(event:MouseEvent) : void
        {
            this._data.pos = this.pos;
            Dispatcher.dispatchEvent(new DataEvent(EventName.PackShowUnLock, this.pos));
            return;
        }// end function

        private function hideUnLockIcon(event:MouseEvent) : void
        {
            this._data.pos = this.pos;
            Dispatcher.dispatchEvent(new DataEvent(EventName.PackHideUnLock, null));
            return;
        }// end function

        private function getTime() : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.GetTime));
            return;
        }// end function

        private function clickHandler(event:MouseEvent) : void
        {
            var _loc_2:Object = null;
            var _loc_3:int = 0;
            var _loc_4:DataProvider = null;
            Log.debug("你单击了物品");
            if (_itemData == null)
            {
                event.stopImmediatePropagation();
                ItemMenuRegister.hideOpList();
            }
            else if (canNotUse)
            {
                MsgManager.showRollTipsMsg(Language.getString(35510));
                event.stopImmediatePropagation();
                ItemMenuRegister.hideOpList();
            }
            else if (this._isTrading)
            {
                MsgManager.showRollTipsMsg(Language.getString(35511));
                event.stopImmediatePropagation();
                ItemMenuRegister.hideOpList();
            }
            else if (event.ctrlKey)
            {
                event.stopImmediatePropagation();
                ItemMenuRegister.hideOpList();
            }
            else if (CursorManager.currentCurSorType == CursorManager.SELL && _itemData.itemInfo)
            {
                event.stopImmediatePropagation();
                ItemMenuRegister.hideOpList();
                if (ItemsUtil.isEmbedGem(itemData))
                {
                    MsgManager.showRollTipsMsg(Language.getString(35512));
                    return;
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopSellItem, itemData));
            }
            else if (CursorManager.currentCurSorType == CursorManager.SAVE && _itemData.itemInfo)
            {
                event.stopImmediatePropagation();
                ItemMenuRegister.hideOpList();
                if (Cache.instance.pack.warehouseCache.isPackFull(itemData.itemCode, itemData.serverData.itemAmount))
                {
                    return MsgManager.showRollTipsMsg(Language.getString(35513), MsgRollTipsType.msgRollTips3);
                }
                _loc_2 = new Object();
                _loc_2.pos = EMoveType._EMoveTypeToWarehouse;
                _loc_2.formPos = EMoveType._EMoveTypeToBag;
                _loc_2.item = this.itemData;
                Dispatcher.dispatchEvent(new DataEvent(EventName.WarehouseToPack, _loc_2));
            }
            else if (this.itemData == PackItemData.unLockItemData)
            {
                event.stopImmediatePropagation();
                _loc_3 = Cache.instance.pack.backPackCache.canOpenGridNum;
                _loc_2 = {posType:EPlayerItemPosType._EPlayerItemPosTypeBag, amount:_loc_3, clientNeedMoney:0};
                Dispatcher.dispatchEvent(new DataEvent(EventName.OpenGrid, _loc_2));
            }
            else if (this.itemData == PackItemData.unLockingItemData)
            {
                ItemFeatureTips.openPackGrid(this._data);
            }
            else if (this.itemData == PackItemData.lockItemData)
            {
                ItemFeatureTips.openPackGrid(this._data);
            }
            else if (this.itemData == PackItemData.needMoneyItemData)
            {
                ItemFeatureTips.openPackGrid(this._data);
            }
            else if (Cache.instance.trade.isTrading)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.TradeClickPackItem, _itemData));
                event.stopImmediatePropagation();
                ItemMenuRegister.hideOpList();
            }
            else if (Cache.instance.guild.selfGuildInfo.wareHouse.isOpen)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_WareHouse_ClickPackItem, _itemData));
                event.stopImmediatePropagation();
                ItemMenuRegister.hideOpList();
            }
            else
            {
                if (GameController.warehouse.isViewShow)
                {
                    return;
                }
                _loc_4 = ItemMenuConst.getEnabeldAttri(ItemMenuConst.getDataProvider(itemData), itemData);
            }
            return;
        }// end function

        private function fastOperate(event:MouseEvent) : void
        {
            var _loc_2:Object = null;
            var _loc_3:DataProvider = null;
            Log.debug("你双击了物品");
            if (_itemData == null || Cache.instance.trade.isTrading)
            {
                event.stopImmediatePropagation();
                ItemMenuRegister.hideOpList();
            }
            else if (CursorManager.currentCurSorType == CursorManager.SELL && _itemData.itemInfo)
            {
                event.stopImmediatePropagation();
                ItemMenuRegister.hideOpList();
            }
            else if (canNotUse)
            {
                MsgManager.showRollTipsMsg(Language.getString(35510));
                event.stopImmediatePropagation();
                ItemMenuRegister.hideOpList();
            }
            else if (this._isTrading)
            {
                MsgManager.showRollTipsMsg(Language.getString(35511));
                event.stopImmediatePropagation();
                ItemMenuRegister.hideOpList();
            }
            else if (Cache.instance.guild.selfGuildInfo.wareHouse.isOpen)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_WareHouse_ClickPackItem, _itemData));
                event.stopImmediatePropagation();
                ItemMenuRegister.hideOpList();
            }
            else
            {
                if (GameController.warehouse.isViewShow)
                {
                    return;
                }
                if (itemData && itemData.itemInfo)
                {
                    _loc_3 = ItemMenuConst.getEnabeldAttri(ItemMenuConst.getDataProvider(itemData), itemData);
                    if ((ItemsUtil.isEquip(itemData) || ItemsUtil.isMountEquip(itemData) || ItemsUtil.isPetEquip(itemData)) && !GameController.playerSystem.isViewShow)
                    {
                        ItemMenuConst.opearte(_loc_3.getItemAt(0)["label"], itemData);
                        event.stopImmediatePropagation();
                        ItemMenuRegister.hideOpList();
                    }
                    else if (_loc_3.length > 0)
                    {
                        ItemMenuConst.opearte(_loc_3.getItemAt(0)["label"], itemData);
                    }
                }
            }
            ItemMenuRegister.hideOpList();
            return;
        }// end function

        private function fastMoveToWareHouse(param1:PackItem, param2:int) : void
        {
            return;
        }// end function

        override protected function onMouseDown(event:MouseEvent) : void
        {
            if (this.itemData && this.isDragAble && !_cannotUse && !this._isTrading)
            {
                DragManager.instance.startDragItem(this, bitmapdata);
            }
            return;
        }// end function

        public function set data(param1:Object) : void
        {
            this._data = param1 as PackObject;
            this._data.pos = this.pos;
            if (this._data.itemData)
            {
                this.itemData = this._data.itemData;
                if (this._data.locked)
                {
                    canNotUse = true;
                }
                else
                {
                    canNotUse = false;
                }
                if (this._data.used)
                {
                    this.isTrading = true;
                }
                else
                {
                    this.isTrading = false;
                }
            }
            else
            {
                this.itemData = null;
                canNotUse = false;
                updateCDEffect();
            }
            return;
        }// end function

        override public function get toolTipData()
        {
            if (itemData == PackItemData.lockItemData || itemData == PackItemData.unLockingItemData || itemData == PackItemData.needMoneyItemData || itemData == PackItemData.unLockItemData)
            {
                return this._data;
            }
            if (itemData)
            {
                return itemData;
            }
            return super.toolTipData;
        }// end function

    }
}
